package com.example.basecommon.utils;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 登录处理工具类
 * Created by zhanlg
 * DATE 2021/5/21
 * TIME 19:30
 */
public class LoginUtil {
    public static String TOKEN = "token";

    public static String SESSIONID = "sessionId";

    /**
     * 手动选择的语言
     */
    public static String SPF_KEY_SELECTED_LOCALE = "selected_locale";

    public static String LOGIN_NAME = "loginName";

    public static String LOGIN_PASSWORD = "loginPassword";

    public static String LOGIN_ACCOUNT = "loginAccount";

    public static String LOGIN_SAVE_PASSWORD = "isSavePassword";

    public static String WAREHOUSE_LIST = "warehouseList";

    public static String CURRENT_WAREHOUSE = "currentWarehouse";

    public static void saveLoginInfo(String acccount, String loginName, String loginPassword, boolean isSavePassword) {
        saveAccountInfo(acccount);
        PreferencesUtils.putString(App.INSTANCE.getApplicationContext(), LOGIN_NAME, loginName);
        if (isSavePassword) {
            PreferencesUtils.putString(App.INSTANCE.getApplicationContext(), LOGIN_PASSWORD, loginPassword);
        } else {
            PreferencesUtils.putString(App.INSTANCE.getApplicationContext(), LOGIN_PASSWORD, "");
        }
        PreferencesUtils.putBoolean(App.INSTANCE.getApplicationContext(), LOGIN_SAVE_PASSWORD, isSavePassword);
    }

    private static void saveAccountInfo(String account) {
        String accountCache = PreferencesUtils.getString(App.INSTANCE.getApplicationContext(), LOGIN_ACCOUNT);
        if (TextUtils.isEmpty(accountCache)) {
            PreferencesUtils.putString(App.INSTANCE.getApplicationContext(), LOGIN_ACCOUNT, account);
        } else {
            if (accountCache.contains(account)) {
                if (!accountCache.startsWith(account)) {
                    accountCache.replace("&" + account, "");
                    String newCache = account + "&" + accountCache;
                    PreferencesUtils.putString(App.INSTANCE.getApplicationContext(), LOGIN_ACCOUNT, newCache);
                }
            } else {
                String newCache = account + "&" + accountCache;
                PreferencesUtils.putString(App.INSTANCE.getApplicationContext(), LOGIN_ACCOUNT, newCache);
            }
        }
    }

    public static List<String> getAccountCache() {
        String accountCache = PreferencesUtils.getString(App.INSTANCE.getApplicationContext(), LOGIN_ACCOUNT);
        if (TextUtils.isEmpty(accountCache)) {
            return new ArrayList<>();
        }
        String[] accountArray = accountCache.split("&");
        List<String> accountList = new ArrayList<>();
        for (int i = 0; i < accountArray.length; i++) {
            accountList.add(accountArray[i]);
        }
        return accountList;
    }

    public static String getToken() {
        return PreferencesUtils.getString(App.INSTANCE.getApplicationContext(), TOKEN, "");
    }


    /**
     * 清除登录信息， 清除密码信息
     */
    public static void clearLoginInfo() {
        PreferencesUtils.putBoolean(App.INSTANCE.getApplicationContext(), LOGIN_SAVE_PASSWORD, false);
        PreferencesUtils.putString(App.INSTANCE.getApplicationContext(), LOGIN_PASSWORD, "");
    }
}
